package com.zhr.emsg.demo.consumer;

import com.smartframework.web.core.mq.bean.MqMsgBean;
import com.smartframework.web.core.mq.handler.MessageConsumer;
import com.smartframework.web.core.runner.CommandExecutor;
import com.smartframework.web.core.runner.ExecutorType;
import com.smartframework.web.core.util.JsonUtil;
import com.zhr.scada.emsg.client.EmsgClient;
import org.apache.log4j.Logger;

import java.util.HashMap;
import java.util.Map;

/**
 * @author LIUJUN
 */
@CommandExecutor(description = "EmsgClient测试消费程序", uri = EmsgClient.EMSG_LOCAL, topics = {"MY_DEMO_REQUEST"}, commandType = ExecutorType.MSG_CONSUMER)
public class MyTopicConsumer extends MessageConsumer {
    public static Logger logger = Logger.getLogger(MyTopicConsumer.class);

    @Override
    protected void handle(String topic, MqMsgBean mqMsgBean) {
        System.out.println(mqMsgBean);
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        try {
            String msgId = String.valueOf(JsonUtil.getMapByJson(String.valueOf(mqMsgBean.getMsgBody())).get("msgId"));
            Object data =new HashMap<String, Object>() {{
                put("feeder", 10000L);
                put("stationId", 100);
                put("newFeederId", "Hello");
            }};
            Map<String,Object> body = new HashMap<>();
            body.put("msgId",msgId);
            body.put("data",data);
            this.getMsgClient().send(topic.replace("_REQUEST", "_RESPONSE"), body);
        } catch (Exception e) {
            e.printStackTrace();
        }
        logger.info("SendOver...");
    }
}
